/*
	EXEC SP_VIEW_FORECAST '1291815410328'
*/
CREATE PROC SP_VIEW_FORECAST( @TRANSACTION_ID VARCHAR(100) ) AS
BEGIN
	DECLARE @IS_MSTER_TABLE INT --0=true, 1=false
	
	CREATE TABLE #QTRS_TABLE ([QUARTER] INT, [YEAR] INT , QTR_LABEL VARCHAR(10), [YEARQTR] INT)
	CREATE TABLE #RESULTS_TABLE ( 
					PROJID INT,
					PRODID INT,
					BU VARCHAR(100),
					PLM VARCHAR(100),
					Product_Segment VARCHAR(100),
					Product_Family VARCHAR(100),
					Chipset VARCHAR(100),
					Sales_Person VARCHAR(100),
					Region VARCHAR(100),
					Customer_Name VARCHAR(100),
					OEM VARCHAR(100),
					ODM VARCHAR(100),
					Carrier VARCHAR(100),
					Chipset_Part_Name VARCHAR(100),
					Marketing_Part_Number VARCHAR(100),
					Manufacturing_Part_Number VARCHAR(100),					
					Chip_Type VARCHAR(100),
					Port_Count VARCHAR(100),
					Q1_Volume INT,
					Q2_Volume INT,
					Q3_Volume INT,
					Q4_Volume INT,
					Q5_Volume INT,
					Q1_ASP FLOAT,
					Q2_ASP FLOAT,
					Q3_ASP FLOAT,
					Q4_ASP FLOAT,
					Q5_ASP FLOAT,
					Q1_Rebates FLOAT,
					Q2_Rebates FLOAT,
					Q3_Rebates FLOAT,
					Q4_Rebates FLOAT,
					Q5_Rebates FLOAT,
					Q1_Material_Costs FLOAT,
					Q2_Material_Costs FLOAT,
					Q3_Material_Costs FLOAT,
					Q4_Material_Costs FLOAT,
					Q5_Material_Costs FLOAT,
					Q1_Royalty FLOAT,
					Q2_Royalty FLOAT,
					Q3_Royalty FLOAT,
					Q4_Royalty FLOAT,
					Q5_Royalty FLOAT,
					UPDATED INT DEFAULT 1, --false
					Q1_PORT_VOLUME FLOAT,
					Q2_PORT_VOLUME FLOAT,
					Q3_PORT_VOLUME FLOAT,
					Q4_PORT_VOLUME FLOAT,
					Q5_PORT_VOLUME FLOAT
					 )

	IF((SELECT COUNT(*) FROM FCENTRYMASTER_ADDITIONAL_DATA_TABLE WHERE TRANSACTION_ID = @TRANSACTION_ID) > 0)
	BEGIN
		SET @IS_MSTER_TABLE = 0
		INSERT INTO #QTRS_TABLE([QUARTER], [YEAR], [QTR_LABEL], [YEARQTR])
			SELECT DISTINCT [QUARTER], YEAR, 'Q' + CONVERT(VARCHAR, [QUARTER]) + SUBSTRING(CONVERT(VARCHAR, [YEAR]), 3, 2), CAST([YEAR] AS VARCHAR)+''+CAST([QUARTER] AS VARCHAR) AS QTRYEAR
				FROM FCENTRYMASTER 
				WHERE USERTXT2 = @TRANSACTION_ID
				ORDER BY CAST([YEAR] AS VARCHAR)+''+CAST([QUARTER] AS VARCHAR) ASC
	END
	ELSE
	BEGIN
		SET @IS_MSTER_TABLE = 1
		INSERT INTO #QTRS_TABLE([QUARTER], [YEAR], [QTR_LABEL], [YEARQTR])
			SELECT DISTINCT [QUARTER], YEAR, 'Q' + CONVERT(VARCHAR, [QUARTER]) + SUBSTRING(CONVERT(VARCHAR, [YEAR]), 3, 2), CAST([YEAR] AS VARCHAR)+''+CAST([QUARTER] AS VARCHAR) AS QTRYEAR
				FROM FCENTRYHISTORY 
				WHERE USERTXT2 = @TRANSACTION_ID
				ORDER BY CAST([YEAR] AS VARCHAR)+''+CAST([QUARTER] AS VARCHAR) ASC
	END

	--SELECT * FROM #QTRS_TABLE	
	
	INSERT INTO #RESULTS_TABLE (PROJID, PRODID, BU, PLM, Product_Segment, Product_Family, Chipset, Sales_Person, Region, Customer_Name, OEM, ODM, Carrier, Chipset_Part_Name, Marketing_Part_Number, Manufacturing_Part_Number, Chip_Type, Port_Count)
		SELECT PS.PROJID, PPR.PRODID, PPADT.BU, PPADT.PLM, PPADT.Product_Segment, PPADT.Product_Family, PPADT.Chipset, 
			(SELECT SALESPERSON_NAME FROM SALES_PERSONS WHERE SALESPERSON_ID = PS.SPERSONID) AS Sales_Person,
			PPADT.Region, 
			(SELECT C_NAME FROM CUSTOMER WHERE CUSTOMER_ID = PS.CUSTID ) AS Customer_Name, 
			(SELECT C_NAME FROM CUSTOMER WHERE CUSTOMER_ID = PS.ENDUSERID ) AS [OEM], 
			(SELECT C_NAME FROM CUSTOMER WHERE CUSTOMER_ID = PS.ODMID ) AS ODM, PPADT.Carrier, PPADT.Chipset_Part_Name,
			(SELECT P_MARKETING_PART_NUMBER FROM PRODUCT WHERE PRODUCT_ID = PPR.PRODID) AS Marketing_Part_Number, 
			(SELECT PART_NUMBER FROM PRODUCT WHERE PRODUCT_ID = PPR.PRODID) AS Manufacturing_Part_Number, 
			PPADT.Chip_Type, PPADT.Port_Count
			 FROM PROJSETUP PS
				JOIN PROJPRODREL PPR ON PS.PROJID = PPR.PROJID
				LEFT OUTER JOIN FCPROJPROD_ADDITIONAL_DATA_TABLE PPADT ON PPR.PROJID = PPADT.PROJID AND PPR.PRODID = PPADT.PRODID

	
	DECLARE @Q1_QTR_NO INT, @Q2_QTR_NO INT, @Q3_QTR_NO INT, @Q4_QTR_NO INT, @Q5_QTR_NO INT
	DECLARE @Q1_YEAR INT, @Q2_YEAR INT, @Q3_YEAR INT, @Q4_YEAR INT, @Q5_YEAR INT
	DECLARE @Q1_LABEL VARCHAR(50), @Q2_LABEL VARCHAR(50), @Q3_LABEL VARCHAR(50), @Q4_LABEL VARCHAR(50), @Q5_LABEL VARCHAR(50)
	DECLARE @QUARTER INT, @YEAR INT, @QTR_LABEL VARCHAR(10)
	DECLARE @CNT INT
	DECLARE @PROJID INT
	DECLARE @PRODID INT
	DECLARE @FC_QTY INT
	DECLARE @FC_PRICE FLOAT
	DECLARE @REBATE FLOAT
	DECLARE @MATERIAL_COST FLOAT
	DECLARE @ROYALTY FLOAT
	DECLARE @PORT_VOLUME FLOAT


	DECLARE @QTRS_CURSOR CURSOR
	SET @QTRS_CURSOR = CURSOR FOR SELECT [QUARTER], [YEAR], [QTR_LABEL] FROM #QTRS_TABLE ORDER BY [YEARQTR] ASC
	OPEN @QTRS_CURSOR 
	FETCH NEXT FROM @QTRS_CURSOR INTO @QUARTER, @YEAR, @QTR_LABEL
	SET @CNT = 1
	WHILE @@FETCH_STATUS = 0
	BEGIN
		IF(@CNT = 1)
		BEGIN
			SET @Q1_QTR_NO = @QUARTER
			SET @Q1_YEAR = @YEAR
			SET @Q1_LABEL = @QTR_LABEL
		END	
		ELSE IF(@CNT = 2)
		BEGIN
			SET @Q2_QTR_NO = @QUARTER
			SET @Q2_YEAR = @YEAR
			SET @Q2_LABEL = @QTR_LABEL
		END	
		ELSE IF(@CNT = 3)
		BEGIN
			SET @Q3_QTR_NO = @QUARTER
			SET @Q3_YEAR = @YEAR
			SET @Q3_LABEL = @QTR_LABEL
		END	
		ELSE IF(@CNT = 4)
		BEGIN
			SET @Q4_QTR_NO = @QUARTER
			SET @Q4_YEAR = @YEAR
			SET @Q4_LABEL = @QTR_LABEL
		END	
		ELSE IF(@CNT = 5)
		BEGIN
			SET @Q5_QTR_NO = @QUARTER
			SET @Q5_YEAR = @YEAR
			SET @Q5_LABEL = @QTR_LABEL
		END	
		SET @CNT = @CNT + 1
		FETCH NEXT FROM @QTRS_CURSOR INTO @QUARTER, @YEAR, @QTR_LABEL
	END
	CLOSE @QTRS_CURSOR
	DEALLOCATE @QTRS_CURSOR

	DECLARE @FC_CURSOR CURSOR
	IF(@IS_MSTER_TABLE = 0)
	BEGIN
		SET @FC_CURSOR = CURSOR FOR SELECT PROJID, PRODID, [QUARTER], [YEAR], QTY, PRICE FROM FCENTRYMASTER FCEM
											WHERE CAST(CONVERT(VARCHAR,FCEM.[YEAR])+CONVERT(VARCHAR,FCEM.[QUARTER]) AS INT)  >= CAST(CONVERT(VARCHAR,@Q1_YEAR)+CONVERT(VARCHAR,@Q1_QTR_NO) AS INT)
											AND CAST(CONVERT(VARCHAR,FCEM.[YEAR])+CONVERT(VARCHAR,FCEM.[QUARTER]) AS INT)  <= CAST(CONVERT(VARCHAR,@Q5_YEAR)+CONVERT(VARCHAR,@Q5_QTR_NO) AS INT)
	END
	ELSE
	BEGIN
		SET @FC_CURSOR = CURSOR FOR SELECT PROJID, PRODID, [QUARTER], [YEAR], QTY, PRICE FROM FCENTRYHISTORY FCEM
											WHERE CAST(CONVERT(VARCHAR,FCEM.[YEAR])+CONVERT(VARCHAR,FCEM.[QUARTER]) AS INT)  >= CAST(CONVERT(VARCHAR,@Q1_YEAR)+CONVERT(VARCHAR,@Q1_QTR_NO) AS INT)
											AND CAST(CONVERT(VARCHAR,FCEM.[YEAR])+CONVERT(VARCHAR,FCEM.[QUARTER]) AS INT)  <= CAST(CONVERT(VARCHAR,@Q5_YEAR)+CONVERT(VARCHAR,@Q5_QTR_NO) AS INT) 
											AND USERTXT2 = @TRANSACTION_ID
	END
	OPEN @FC_CURSOR 
	FETCH NEXT FROM @FC_CURSOR INTO @PROJID, @PRODID, @QUARTER, @YEAR, @FC_QTY, @FC_PRICE
	WHILE (@@FETCH_STATUS = 0)
	BEGIN
		IF( @QUARTER = @Q1_QTR_NO AND @YEAR = @Q1_YEAR )
			UPDATE #RESULTS_TABLE SET Q1_Volume = @FC_QTY, Q1_ASP = @FC_PRICE, UPDATED = 0 WHERE PROJID = @PROJID AND PRODID = @PRODID
		ELSE IF( @QUARTER = @Q2_QTR_NO AND @YEAR = @Q2_YEAR )
			UPDATE #RESULTS_TABLE SET Q2_Volume = @FC_QTY, Q2_ASP = @FC_PRICE, UPDATED = 0 WHERE PROJID = @PROJID AND PRODID = @PRODID
		ELSE IF( @QUARTER = @Q3_QTR_NO AND @YEAR = @Q3_YEAR )
			UPDATE #RESULTS_TABLE SET Q3_Volume = @FC_QTY, Q3_ASP = @FC_PRICE, UPDATED = 0 WHERE PROJID = @PROJID AND PRODID = @PRODID
		ELSE IF( @QUARTER = @Q4_QTR_NO AND @YEAR = @Q4_YEAR )
			UPDATE #RESULTS_TABLE SET Q4_Volume = @FC_QTY, Q4_ASP = @FC_PRICE, UPDATED = 0 WHERE PROJID = @PROJID AND PRODID = @PRODID
		ELSE IF( @QUARTER = @Q5_QTR_NO AND @YEAR = @Q5_YEAR )
			UPDATE #RESULTS_TABLE SET Q5_Volume = @FC_QTY, Q5_ASP = @FC_PRICE, UPDATED = 0 WHERE PROJID = @PROJID AND PRODID = @PRODID

		FETCH NEXT FROM @FC_CURSOR INTO @PROJID, @PRODID, @QUARTER, @YEAR, @FC_QTY, @FC_PRICE	
	END
	CLOSE @FC_CURSOR
	DEALLOCATE @FC_CURSOR

	DECLARE @FC_ADDITIONAL_CURSOR CURSOR
	IF(@IS_MSTER_TABLE = 0)
	BEGIN
		SET @FC_ADDITIONAL_CURSOR = CURSOR FOR SELECT PROJID, PRODID, [QUARTER], [YEAR], REBATE, MATERIAL_COST, ROYALTY, PORT_VOLUME
											FROM [FCENTRYMASTER_ADDITIONAL_DATA_TABLE] FCAD
											WHERE CAST(CONVERT(VARCHAR,FCAD.[YEAR])+CONVERT(VARCHAR,FCAD.[QUARTER]) AS INT)  >= CAST(CONVERT(VARCHAR,@Q1_YEAR)+CONVERT(VARCHAR,@Q1_QTR_NO) AS INT)
											AND CAST(CONVERT(VARCHAR,FCAD.[YEAR])+CONVERT(VARCHAR,FCAD.[QUARTER]) AS INT)  <= CAST(CONVERT(VARCHAR,@Q5_YEAR)+CONVERT(VARCHAR,@Q5_QTR_NO) AS INT)
	END
	ELSE
	BEGIN
		SET @FC_ADDITIONAL_CURSOR = CURSOR FOR SELECT PROJID, PRODID, [QUARTER], [YEAR], REBATE, MATERIAL_COST, ROYALTY, PORT_VOLUME
											FROM FCENTRYMASTER_ADDITIONAL_DATA_HISTORY_TABLE FCAD
											WHERE CAST(CONVERT(VARCHAR,FCAD.[YEAR])+CONVERT(VARCHAR,FCAD.[QUARTER]) AS INT)  >= CAST(CONVERT(VARCHAR,@Q1_YEAR)+CONVERT(VARCHAR,@Q1_QTR_NO) AS INT)
											AND CAST(CONVERT(VARCHAR,FCAD.[YEAR])+CONVERT(VARCHAR,FCAD.[QUARTER]) AS INT)  <= CAST(CONVERT(VARCHAR,@Q5_YEAR)+CONVERT(VARCHAR,@Q5_QTR_NO) AS INT)
											AND TRANSACTION_ID = @TRANSACTION_ID		
	END
	OPEN @FC_ADDITIONAL_CURSOR 
	FETCH NEXT FROM @FC_ADDITIONAL_CURSOR INTO @PROJID, @PRODID, @QUARTER, @YEAR, @REBATE, @MATERIAL_COST, @ROYALTY, @PORT_VOLUME
	WHILE (@@FETCH_STATUS = 0)
	BEGIN
		IF( @QUARTER = @Q1_QTR_NO AND @YEAR = @Q1_YEAR )
			UPDATE #RESULTS_TABLE SET Q1_Rebates = @REBATE, Q1_Material_Costs = @MATERIAL_COST, Q1_Royalty = @ROYALTY, UPDATED = 0, Q1_PORT_VOLUME = @PORT_VOLUME
						WHERE PROJID = @PROJID AND PRODID = @PRODID
		ELSE IF( @QUARTER = @Q2_QTR_NO AND @YEAR = @Q2_YEAR )
			UPDATE #RESULTS_TABLE SET Q2_Rebates = @REBATE, Q2_Material_Costs = @MATERIAL_COST, Q2_Royalty = @ROYALTY, UPDATED = 0, Q2_PORT_VOLUME = @PORT_VOLUME
						WHERE PROJID = @PROJID AND PRODID = @PRODID
		ELSE IF( @QUARTER = @Q3_QTR_NO AND @YEAR = @Q3_YEAR )
			UPDATE #RESULTS_TABLE SET Q3_Rebates = @REBATE, Q3_Material_Costs = @MATERIAL_COST, Q3_Royalty = @ROYALTY, UPDATED = 0, Q3_PORT_VOLUME = @PORT_VOLUME
						WHERE PROJID = @PROJID AND PRODID = @PRODID
		ELSE IF( @QUARTER = @Q4_QTR_NO AND @YEAR = @Q4_YEAR )
			UPDATE #RESULTS_TABLE SET Q4_Rebates = @REBATE, Q4_Material_Costs = @MATERIAL_COST, Q4_Royalty = @ROYALTY, UPDATED = 0, Q4_PORT_VOLUME = @PORT_VOLUME
						WHERE PROJID = @PROJID AND PRODID = @PRODID
		ELSE IF( @QUARTER = @Q5_QTR_NO AND @YEAR = @Q5_YEAR )
			UPDATE #RESULTS_TABLE SET Q5_Rebates = @REBATE, Q5_Material_Costs = @MATERIAL_COST, Q5_Royalty = @ROYALTY, UPDATED = 0, Q5_PORT_VOLUME = @PORT_VOLUME
						WHERE PROJID = @PROJID AND PRODID = @PRODID

		FETCH NEXT FROM @FC_ADDITIONAL_CURSOR INTO @PROJID, @PRODID, @QUARTER, @YEAR, @REBATE, @MATERIAL_COST, @ROYALTY, @PORT_VOLUME	
	END
	CLOSE @FC_ADDITIONAL_CURSOR
	DEALLOCATE @FC_ADDITIONAL_CURSOR

	DECLARE @QUERY NVARCHAR(MAX)
	DECLARE @Q1_VOLUME_LABEL VARCHAR(50), @Q2_VOLUME_LABEL VARCHAR(50), @Q3_VOLUME_LABEL VARCHAR(50), @Q4_VOLUME_LABEL VARCHAR(50), @Q5_VOLUME_LABEL VARCHAR(50)
	DECLARE @Q1_ASP_LABEL VARCHAR(50), @Q2_ASP_LABEL VARCHAR(50), @Q3_ASP_LABEL VARCHAR(50), @Q4_ASP_LABEL VARCHAR(50), @Q5_ASP_LABEL VARCHAR(50)
	DECLARE @Q1_GROSS_REVENUE_LABEL VARCHAR(50), @Q2_GROSS_REVENUE_LABEL VARCHAR(50), @Q3_GROSS_REVENUE_LABEL VARCHAR(50), @Q4_GROSS_REVENUE_LABEL VARCHAR(50), @Q5_GROSS_REVENUE_LABEL VARCHAR(50)
	DECLARE @Q1_REBATES_LABEL VARCHAR(50), @Q2_REBATES_LABEL VARCHAR(50), @Q3_REBATES_LABEL VARCHAR(50), @Q4_REBATES_LABEL VARCHAR(50), @Q5_REBATES_LABEL VARCHAR(50)
	DECLARE @Q1_NET_ASP_LABEL VARCHAR(50), @Q2_NET_ASP_LABEL VARCHAR(50), @Q3_NET_ASP_LABEL VARCHAR(50), @Q4_NET_ASP_LABEL VARCHAR(50), @Q5_NET_ASP_LABEL VARCHAR(50)
	DECLARE @Q1_NET_REVENUE_LABEL VARCHAR(50), @Q2_NET_REVENUE_LABEL VARCHAR(50), @Q3_NET_REVENUE_LABEL VARCHAR(50), @Q4_NET_REVENUE_LABEL VARCHAR(50), @Q5_NET_REVENUE_LABEL VARCHAR(50)
	DECLARE @Q1_MAT_COST_LABEL VARCHAR(50), @Q2_MAT_COST_LABEL VARCHAR(50), @Q3_MAT_COST_LABEL VARCHAR(50), @Q4_MAT_COST_LABEL VARCHAR(50), @Q5_MAT_COST_LABEL VARCHAR(50)
	DECLARE @Q1_ROYALTY_LABEL VARCHAR(50), @Q2_ROYALTY_LABEL VARCHAR(50), @Q3_ROYALTY_LABEL VARCHAR(50), @Q4_ROYALTY_LABEL VARCHAR(50), @Q5_ROYALTY_LABEL VARCHAR(50)
	DECLARE @Q1_OH_LABEL VARCHAR(50), @Q2_OH_LABEL VARCHAR(50), @Q3_OH_LABEL VARCHAR(50), @Q4_OH_LABEL VARCHAR(50), @Q5_OH_LABEL VARCHAR(50)
	DECLARE @Q1_COGS_PER_UNIT_LABEL VARCHAR(50), @Q2_COGS_PER_UNIT_LABEL VARCHAR(50), @Q3_COGS_PER_UNIT_LABEL VARCHAR(50), @Q4_COGS_PER_UNIT_LABEL VARCHAR(50), @Q5_COGS_PER_UNIT_LABEL VARCHAR(50)
	DECLARE @Q1_COGS_LABEL VARCHAR(50), @Q2_COGS_LABEL VARCHAR(50), @Q3_COGS_LABEL VARCHAR(50), @Q4_COGS_LABEL VARCHAR(50), @Q5_COGS_LABEL VARCHAR(50)
	DECLARE @Q1_GM_LABEL VARCHAR(50), @Q2_GM_LABEL VARCHAR(50), @Q3_GM_LABEL VARCHAR(50), @Q4_GM_LABEL VARCHAR(50), @Q5_GM_LABEL VARCHAR(50)
	DECLARE @Q1_GM_PER_LABEL VARCHAR(50), @Q2_GM_PER_LABEL VARCHAR(50), @Q3_GM_PER_LABEL VARCHAR(50), @Q4_GM_PER_LABEL VARCHAR(50), @Q5_GM_PER_LABEL VARCHAR(50)
	DECLARE @Q1_NM_LABEL VARCHAR(50), @Q2_NM_LABEL VARCHAR(50), @Q3_NM_LABEL VARCHAR(50), @Q4_NM_LABEL VARCHAR(50), @Q5_NM_LABEL VARCHAR(50)
	DECLARE @Q1_NM_PER_LABEL VARCHAR(50), @Q2_NM_PER_LABEL VARCHAR(50), @Q3_NM_PER_LABEL VARCHAR(50), @Q4_NM_PER_LABEL VARCHAR(50), @Q5_NM_PER_LABEL VARCHAR(50)
	DECLARE @Q1_PORT_VOLUME_LABEL VARCHAR(50), @Q2_PORT_VOLUME_LABEL VARCHAR(50), @Q3_PORT_VOLUME_LABEL VARCHAR(50), @Q4_PORT_VOLUME_LABEL VARCHAR(50), @Q5_PORT_VOLUME_LABEL VARCHAR(50)

	SET @Q1_VOLUME_LABEL = @Q1_LABEL + ' Volume' 
	SET @Q2_VOLUME_LABEL = @Q2_LABEL + ' Volume' 
	SET @Q3_VOLUME_LABEL = @Q3_LABEL + ' Volume' 
	SET @Q4_VOLUME_LABEL = @Q4_LABEL + ' Volume' 
	SET @Q5_VOLUME_LABEL = @Q5_LABEL + ' Volume' 

	SET @Q1_ASP_LABEL = @Q1_LABEL + ' ASP' 
	SET @Q2_ASP_LABEL = @Q2_LABEL + ' ASP' 
	SET @Q3_ASP_LABEL = @Q3_LABEL + ' ASP' 
	SET @Q4_ASP_LABEL = @Q4_LABEL + ' ASP' 
	SET @Q5_ASP_LABEL = @Q5_LABEL + ' ASP' 

	SET @Q1_GROSS_REVENUE_LABEL = @Q1_LABEL + ' Gross Revenue' 
	SET @Q2_GROSS_REVENUE_LABEL = @Q2_LABEL + ' Gross Revenue' 
	SET @Q3_GROSS_REVENUE_LABEL = @Q3_LABEL + ' Gross Revenue' 
	SET @Q4_GROSS_REVENUE_LABEL = @Q4_LABEL + ' Gross Revenue' 
	SET @Q5_GROSS_REVENUE_LABEL = @Q5_LABEL + ' Gross Revenue' 

	SET @Q1_REBATES_LABEL = @Q1_LABEL + ' Rebates' 
	SET @Q2_REBATES_LABEL = @Q2_LABEL + ' Rebates' 
	SET @Q3_REBATES_LABEL = @Q3_LABEL + ' Rebates' 
	SET @Q4_REBATES_LABEL = @Q4_LABEL + ' Rebates' 
	SET @Q5_REBATES_LABEL = @Q5_LABEL + ' Rebates' 

	SET @Q1_NET_ASP_LABEL = @Q1_LABEL + ' Net ASP' 
	SET @Q2_NET_ASP_LABEL = @Q2_LABEL + ' Net ASP' 
	SET @Q3_NET_ASP_LABEL = @Q3_LABEL + ' Net ASP' 
	SET @Q4_NET_ASP_LABEL = @Q4_LABEL + ' Net ASP' 
	SET @Q5_NET_ASP_LABEL = @Q5_LABEL + ' Net ASP' 

	SET @Q1_NET_REVENUE_LABEL = @Q1_LABEL + ' Net Revenue' 
	SET @Q2_NET_REVENUE_LABEL = @Q2_LABEL + ' Net Revenue' 
	SET @Q3_NET_REVENUE_LABEL = @Q3_LABEL + ' Net Revenue' 
	SET @Q4_NET_REVENUE_LABEL = @Q4_LABEL + ' Net Revenue' 
	SET @Q5_NET_REVENUE_LABEL = @Q5_LABEL + ' Net Revenue' 

	SET @Q1_MAT_COST_LABEL = @Q1_LABEL + ' Material Costs' 
	SET @Q2_MAT_COST_LABEL = @Q2_LABEL + ' Material Costs' 
	SET @Q3_MAT_COST_LABEL = @Q3_LABEL + ' Material Costs' 
	SET @Q4_MAT_COST_LABEL = @Q4_LABEL + ' Material Costs' 
	SET @Q5_MAT_COST_LABEL = @Q5_LABEL + ' Material Costs'

	SET @Q1_ROYALTY_LABEL = @Q1_LABEL + ' Royalty' 
	SET @Q2_ROYALTY_LABEL = @Q2_LABEL + ' Royalty' 
	SET @Q3_ROYALTY_LABEL = @Q3_LABEL + ' Royalty' 
	SET @Q4_ROYALTY_LABEL = @Q4_LABEL + ' Royalty' 
	SET @Q5_ROYALTY_LABEL = @Q5_LABEL + ' Royalty'

	SET @Q1_OH_LABEL = @Q1_LABEL + ' OH' 
	SET @Q2_OH_LABEL = @Q2_LABEL + ' OH' 
	SET @Q3_OH_LABEL = @Q3_LABEL + ' OH' 
	SET @Q4_OH_LABEL = @Q4_LABEL + ' OH' 
	SET @Q5_OH_LABEL = @Q5_LABEL + ' OH'

	SET @Q1_COGS_PER_UNIT_LABEL = @Q1_LABEL + ' COGS/Unit' 
	SET @Q2_COGS_PER_UNIT_LABEL = @Q2_LABEL + ' COGS/Unit' 
	SET @Q3_COGS_PER_UNIT_LABEL = @Q3_LABEL + ' COGS/Unit' 
	SET @Q4_COGS_PER_UNIT_LABEL = @Q4_LABEL + ' COGS/Unit' 
	SET @Q5_COGS_PER_UNIT_LABEL = @Q5_LABEL + ' COGS/Unit'

	SET @Q1_COGS_LABEL = @Q1_LABEL + ' COGS' 
	SET @Q2_COGS_LABEL = @Q2_LABEL + ' COGS' 
	SET @Q3_COGS_LABEL = @Q3_LABEL + ' COGS' 
	SET @Q4_COGS_LABEL = @Q4_LABEL + ' COGS' 
	SET @Q5_COGS_LABEL = @Q5_LABEL + ' COGS'

	SET @Q1_GM_LABEL = @Q1_LABEL + ' GM' 
	SET @Q2_GM_LABEL = @Q2_LABEL + ' GM' 
	SET @Q3_GM_LABEL = @Q3_LABEL + ' GM' 
	SET @Q4_GM_LABEL = @Q4_LABEL + ' GM' 
	SET @Q5_GM_LABEL = @Q5_LABEL + ' GM'

	SET @Q1_GM_PER_LABEL = @Q1_LABEL + ' GM%' 
	SET @Q2_GM_PER_LABEL = @Q2_LABEL + ' GM%' 
	SET @Q3_GM_PER_LABEL = @Q3_LABEL + ' GM%' 
	SET @Q4_GM_PER_LABEL = @Q4_LABEL + ' GM%' 
	SET @Q5_GM_PER_LABEL = @Q5_LABEL + ' GM%'

	SET @Q1_NM_LABEL = @Q1_LABEL + ' NM' 
	SET @Q2_NM_LABEL = @Q2_LABEL + ' NM' 
	SET @Q3_NM_LABEL = @Q3_LABEL + ' NM' 
	SET @Q4_NM_LABEL = @Q4_LABEL + ' NM' 
	SET @Q5_NM_LABEL = @Q5_LABEL + ' NM'

	SET @Q1_NM_PER_LABEL = @Q1_LABEL + ' NM%' 
	SET @Q2_NM_PER_LABEL = @Q2_LABEL + ' NM%' 
	SET @Q3_NM_PER_LABEL = @Q3_LABEL + ' NM%' 
	SET @Q4_NM_PER_LABEL = @Q4_LABEL + ' NM%' 
	SET @Q5_NM_PER_LABEL = @Q5_LABEL + ' NM%'

	SET @Q1_PORT_VOLUME_LABEL = @Q1_LABEL + ' Port Volume' 
	SET @Q2_PORT_VOLUME_LABEL = @Q2_LABEL + ' Port Volume' 
	SET @Q3_PORT_VOLUME_LABEL = @Q3_LABEL + ' Port Volume' 
	SET @Q4_PORT_VOLUME_LABEL = @Q4_LABEL + ' Port Volume'
	SET @Q5_PORT_VOLUME_LABEL = @Q5_LABEL + ' Port Volume'

	SET @QUERY = N'SELECT BU AS [BU],
		PLM AS [PLM],
		Product_Segment AS [Product Segment],
		Product_Family AS [Product Family],
		Chipset AS [Chipset],
		Sales_Person AS [Sales Person],
		Region AS [Region],
		Customer_Name AS [Customer Name],
		OEM AS [OEM] ,
		ODM AS [ODM],
		Carrier AS [Carrier],
		Chipset_Part_Name AS [Chipset Part Name],
		Marketing_Part_Number AS [Marketing Part Number],
		Manufacturing_Part_Number AS [Manufacturing Part Number],					
		Chip_Type AS [Chip Type],
		Port_Count AS [Port Count],
		
		Q1_Volume AS [' + @Q1_VOLUME_LABEL + '],
		Q2_Volume AS [' + @Q2_VOLUME_LABEL + '],
		Q3_Volume AS [' + @Q3_VOLUME_LABEL + '],
		Q4_Volume AS [' + @Q4_VOLUME_LABEL + '],
		Q5_Volume AS [' + @Q5_VOLUME_LABEL + '],
		Q1_ASP AS [' + @Q1_ASP_LABEL + '],
		Q2_ASP AS [' + @Q2_ASP_LABEL + '],
		Q3_ASP AS [' + @Q3_ASP_LABEL + '],
		Q4_ASP AS [' + @Q4_ASP_LABEL + '],
		Q5_ASP AS [' + @Q5_ASP_LABEL + '],
		'+'''${'+@Q1_VOLUME_LABEL+'}*${'+@Q1_ASP_LABEL+'}'' AS [' + @Q1_GROSS_REVENUE_LABEL + '],
		'+'''${'+@Q2_VOLUME_LABEL+'}*${'+@Q2_ASP_LABEL+'}'' AS [' + @Q2_GROSS_REVENUE_LABEL + '],
		'+'''${'+@Q3_VOLUME_LABEL+'}*${'+@Q3_ASP_LABEL+'}'' AS [' + @Q3_GROSS_REVENUE_LABEL + '],
		'+'''${'+@Q4_VOLUME_LABEL+'}*${'+@Q4_ASP_LABEL+'}'' AS [' + @Q4_GROSS_REVENUE_LABEL + '],
		'+'''${'+@Q5_VOLUME_LABEL+'}*${'+@Q5_ASP_LABEL+'}'' AS [' + @Q5_GROSS_REVENUE_LABEL + '],
		Q1_Rebates AS [' + @Q1_REBATES_LABEL + '],
		Q2_Rebates AS [' + @Q2_REBATES_LABEL + '],
		Q3_Rebates AS [' + @Q3_REBATES_LABEL + '],
		Q4_Rebates AS [' + @Q4_REBATES_LABEL + '],
		Q5_Rebates AS [' + @Q5_REBATES_LABEL + '],
		
		'+'''${'+@Q1_ASP_LABEL+'}-${'+@Q1_REBATES_LABEL+'}'' AS [' + @Q1_NET_ASP_LABEL + '],
		'+'''${'+@Q2_ASP_LABEL+'}-${'+@Q2_REBATES_LABEL+'}'' AS [' + @Q2_NET_ASP_LABEL + '],
		'+'''${'+@Q3_ASP_LABEL+'}-${'+@Q3_REBATES_LABEL+'}'' AS [' + @Q3_NET_ASP_LABEL + '],
		'+'''${'+@Q4_ASP_LABEL+'}-${'+@Q4_REBATES_LABEL+'}'' AS [' + @Q4_NET_ASP_LABEL + '],
		'+'''${'+@Q5_ASP_LABEL+'}-${'+@Q5_REBATES_LABEL+'}'' AS [' + @Q5_NET_ASP_LABEL + '],

		'+'''${'+@Q1_VOLUME_LABEL+'}*${'+@Q1_NET_ASP_LABEL+'}'' AS [' + @Q1_NET_REVENUE_LABEL + '],
		'+'''${'+@Q2_VOLUME_LABEL+'}*${'+@Q2_NET_ASP_LABEL+'}'' AS [' + @Q2_NET_REVENUE_LABEL + '],
		'+'''${'+@Q3_VOLUME_LABEL+'}*${'+@Q3_NET_ASP_LABEL+'}'' AS [' + @Q3_NET_REVENUE_LABEL + '],
		'+'''${'+@Q4_VOLUME_LABEL+'}*${'+@Q4_NET_ASP_LABEL+'}'' AS [' + @Q4_NET_REVENUE_LABEL + '],		
		'+'''${'+@Q5_VOLUME_LABEL+'}*${'+@Q5_NET_ASP_LABEL+'}'' AS [' + @Q5_NET_REVENUE_LABEL + '],
		
		Q1_Material_Costs AS [' + @Q1_MAT_COST_LABEL + '],
		Q2_Material_Costs AS [' + @Q2_MAT_COST_LABEL + '],
		Q3_Material_Costs AS [' + @Q3_MAT_COST_LABEL + '],
		Q4_Material_Costs AS [' + @Q4_MAT_COST_LABEL + '],
		Q5_Material_Costs AS [' + @Q5_MAT_COST_LABEL + '],
		Q1_Royalty AS [' + @Q1_ROYALTY_LABEL + '],
		Q2_Royalty AS [' + @Q2_ROYALTY_LABEL + '],
		Q3_Royalty AS [' + @Q3_ROYALTY_LABEL + '],
		Q4_Royalty AS [' + @Q4_ROYALTY_LABEL + '],
		Q5_Royalty AS [' + @Q5_ROYALTY_LABEL + '],

		'+'''(${'+@Q1_MAT_COST_LABEL+'}+${'+@Q1_ROYALTY_LABEL+'})*0.05'' AS [' + @Q1_OH_LABEL + '],
		'+'''(${'+@Q2_MAT_COST_LABEL+'}+${'+@Q2_ROYALTY_LABEL+'})*0.05'' AS [' + @Q2_OH_LABEL + '],
		'+'''(${'+@Q3_MAT_COST_LABEL+'}+${'+@Q3_ROYALTY_LABEL+'})*0.05'' AS [' + @Q3_OH_LABEL + '],
		'+'''(${'+@Q4_MAT_COST_LABEL+'}+${'+@Q4_ROYALTY_LABEL+'})*0.05'' AS [' + @Q4_OH_LABEL + '],
		'+'''(${'+@Q5_MAT_COST_LABEL+'}+${'+@Q5_ROYALTY_LABEL+'})*0.05'' AS [' + @Q5_OH_LABEL + '],
		
		'+'''(${'+@Q1_MAT_COST_LABEL+'}+${'+@Q1_ROYALTY_LABEL+'}+${'+ @Q1_OH_LABEL +'})'' AS [' + @Q1_COGS_PER_UNIT_LABEL + '],
		'+'''(${'+@Q2_MAT_COST_LABEL+'}+${'+@Q2_ROYALTY_LABEL+'}+${'+ @Q2_OH_LABEL +'})'' AS [' + @Q2_COGS_PER_UNIT_LABEL + '],
		'+'''(${'+@Q3_MAT_COST_LABEL+'}+${'+@Q3_ROYALTY_LABEL+'}+${'+ @Q3_OH_LABEL +'})'' AS [' + @Q3_COGS_PER_UNIT_LABEL + '],
		'+'''(${'+@Q4_MAT_COST_LABEL+'}+${'+@Q4_ROYALTY_LABEL+'}+${'+ @Q4_OH_LABEL +'})'' AS [' + @Q4_COGS_PER_UNIT_LABEL + '],	
		'+'''(${'+@Q5_MAT_COST_LABEL+'}+${'+@Q5_ROYALTY_LABEL+'}+${'+ @Q5_OH_LABEL +'})'' AS [' + @Q5_COGS_PER_UNIT_LABEL + '],	

		'+'''${'+@Q1_COGS_PER_UNIT_LABEL+'}*${'+@Q1_VOLUME_LABEL+'}'' AS [' + @Q1_COGS_LABEL + '],
		'+'''${'+@Q2_COGS_PER_UNIT_LABEL+'}*${'+@Q2_VOLUME_LABEL+'}'' AS [' + @Q2_COGS_LABEL + '],
		'+'''${'+@Q3_COGS_PER_UNIT_LABEL+'}*${'+@Q3_VOLUME_LABEL+'}'' AS [' + @Q3_COGS_LABEL + '],
		'+'''${'+@Q4_COGS_PER_UNIT_LABEL+'}*${'+@Q4_VOLUME_LABEL+'}'' AS [' + @Q4_COGS_LABEL + '],
		'+'''${'+@Q5_COGS_PER_UNIT_LABEL+'}*${'+@Q5_VOLUME_LABEL+'}'' AS [' + @Q5_COGS_LABEL + '],

		'+'''${'+@Q1_GROSS_REVENUE_LABEL+'}-${'+@Q1_COGS_LABEL+'}'' AS [' + @Q1_GM_LABEL + '],
		'+'''${'+@Q2_GROSS_REVENUE_LABEL+'}-${'+@Q2_COGS_LABEL+'}'' AS [' + @Q2_GM_LABEL + '],
		'+'''${'+@Q3_GROSS_REVENUE_LABEL+'}-${'+@Q3_COGS_LABEL+'}'' AS [' + @Q3_GM_LABEL + '],
		'+'''${'+@Q4_GROSS_REVENUE_LABEL+'}-${'+@Q4_COGS_LABEL+'}'' AS [' + @Q4_GM_LABEL + '],
		'+'''${'+@Q5_GROSS_REVENUE_LABEL+'}-${'+@Q5_COGS_LABEL+'}'' AS [' + @Q5_GM_LABEL + '],

		'+'''${'+ @Q1_NET_REVENUE_LABEL+'}-${'+@Q1_COGS_LABEL+'}'' AS [' + @Q1_NM_LABEL + '],
		'+'''${'+ @Q2_NET_REVENUE_LABEL+'}-${'+@Q2_COGS_LABEL+'}'' AS [' + @Q2_NM_LABEL + '],
		'+'''${'+ @Q3_NET_REVENUE_LABEL+'}-${'+@Q3_COGS_LABEL+'}'' AS [' + @Q3_NM_LABEL + '],
		'+'''${'+ @Q4_NET_REVENUE_LABEL+'}-${'+@Q4_COGS_LABEL+'}'' AS [' + @Q4_NM_LABEL + '],
		'+'''${'+ @Q5_NET_REVENUE_LABEL+'}-${'+@Q5_COGS_LABEL+'}'' AS [' + @Q5_NM_LABEL + '],
		
		Q1_PORT_VOLUME AS [' + @Q1_PORT_VOLUME_LABEL + '],
		Q2_PORT_VOLUME AS [' + @Q2_PORT_VOLUME_LABEL + '],
		Q3_PORT_VOLUME AS [' + @Q3_PORT_VOLUME_LABEL + '],
		Q4_PORT_VOLUME AS [' + @Q4_PORT_VOLUME_LABEL + '],
		Q5_PORT_VOLUME AS [' + @Q5_PORT_VOLUME_LABEL + ']'

	SET  @QUERY = @QUERY + N' FROM #RESULTS_TABLE WHERE UPDATED = 0'
	
	EXEC (@QUERY)
END